/* GUI CONVERTED from rcctwbc1.p (converter v1.76) Tue Aug 20 15:41:42 2002 */
/* rcctwbc1.p  - CONTAINER WORKBENCH - SUB PROGRAM - ADD CONTAINER            */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.8.1.17 $                                                      */
/*V8:ConvertMode=Maintenance                                                  */
/* VERSION 7.5      LAST MODIFIED: 04/14/95           BY: GWM  *J049*         */
/* VERSION 8.5      LAST MODIFIED: 04/12/96           BY: GWM  *J0J1*         */
/* VERSION 8.5      LAST MODIFIED: 06/11/96           BY: rxm  *G1XQ*         */
/* REVISION: 8.6    LAST MODIFIED: 08/03/96   BY: *K003* Vinay Nayak-Sujir    */
/* REVISION: 8.6    LAST MODIFIED: 10/03/96   BY: *K003* Kieu Nguyen          */
/* VERSION 8.6      LAST MODIFIED: 04/09/97   BY: *J1NG* Ajit Deodhar         */
/* VERSION 8.6      LAST MODIFIED: 08/21/97   BY: *J1ZC* Aruna Patil          */
/* REVISION: 8.6E   LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane            */
/* VERSION 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan           */
/* VERSION 8.6E     LAST MODIFIED: 05/20/98   BY: *J2RZ* Dana Tunstall        */
/* REVISION: 8.6E   LAST MODIFIED: 07/22/98   BY: *J2M7* Niranjan R.          */
/* REVISION: 8.6E   LAST MODIFIED: 08/18/99   BY: *K223* Santosh Rao          */
/* REVISION: 8.6E   LAST MODIFIED: 09/16/99   BY: *K21D* Poonam Bahl          */
/* REVISION: 8.6E   LAST MODIFIED: 10/29/99   BY: *K242* Kedar Deherkar       */
/* REVISION: 9.1    LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane     */
/* REVISION: 9.1    LAST MODIFIED: 07/19/00   BY: *N0GF* Mudit Mehta          */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown           */
/* Revision: 1.8.1.12     BY: Jean Miller       DATE: 03/22/01 ECO: *P008*    */
/* Revision: 1.8.1.13     BY: Steve Nugent      DATE: 07/09/01 ECO: *P007*    */
/* Revision: 1.8.1.14     BY: Rajaneesh S.      DATE: 02/21/02 ECO: *L13N*    */
/* Revision: 1.8.1.16  BY: Ashish Maheshwari DATE: 07/17/02 ECO: *N1GJ*    */
/* $Revision: 1.8.1.17 $  BY: Samir Bavkar    DATE: 08/15/02 ECO: *P09K*   */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE rcctwbc1_p_1 "UM"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_2 "Volume UM"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_3 "Volume"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_5 "Ref"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_6 " Container Information "
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_7 "Container Item"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_8 "Multi Entry"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_10 "Next Container"
/* MaxLen: Comment: */

&SCOPED-DEFINE rcctwbc1_p_11 "Tare Weight"
/* MaxLen: 17  Comment: Label for l_pt_ship_wt */

/* ********** End Translatable Strings Definitions ********* */

{rcinvtbl.i new}

/* INPUT PARAMETERS */
define input parameter ship_from as character.
define input parameter temp_parent as character.
define input parameter cont_level as integer.
define input parameter parent_number as character.
define input parameter add_item as logical.
define input parameter tmp_prefix as character.
define input parameter vShipToCode as character.

/* OUTPUT PARAMETERS */
define output parameter abnormal_exit as logical.

/* SHARED VARIABLES */
define new shared variable new_site as character.
define new shared variable so_db as character.
define new shared variable err_stat as integer.
define new shared variable multi_entry as logical no-undo.
define new shared variable cline as character.
define new shared variable lotserial_control like pt_lot_ser.
define new shared variable issue_or_receipt as character.
define new shared variable total_lotserial_qty like sr_qty.
define new shared variable site like sr_site no-undo.
define new shared variable location like sr_loc no-undo.
define new shared variable lotserial like sr_lotser no-undo.
define new shared variable lotserial_qty like sr_qty no-undo.
define new shared variable trans_um like pt_um.
define new shared variable trans_conv like sod_um_conv.
define new shared variable transtype as character initial "ISS-UNP".
define new shared variable lotref like sr_ref no-undo.

/* LOCAL VARIABLES */
define variable num_containers    as integer format "->>>9"
                                  label "Quantity" no-undo.
define variable beg_cont_num       as character format "x(8)"
                                   label {&rcctwbc1_p_10} no-undo.
define variable container_item     like pt_part
                                   label {&rcctwbc1_p_7} no-undo.
define variable cont_site          like abs_site no-undo.
define variable item_description   like pt_desc1 format "x(20)" no-undo.
define variable item_description_2 like pt_desc2 format "x(20)" no-undo.

define variable item_um as character format "XX" no-undo.

define variable cont_loc     like abs_loc no-undo.
define variable cont_wght    like pt_net_wt no-undo.
define variable cont_wt_um   like pt_net_wt_um no-undo.
define variable cont_vol     like pt_size     label {&rcctwbc1_p_3} no-undo.
define variable cont_vol_um  like pt_size_um  label {&rcctwbc1_p_2} no-undo.
define variable cont_qty     as integer no-undo.
define variable cont_lot_ser like abs_lot no-undo.
define variable cont_ref     like abs_ref     label {&rcctwbc1_p_5} no-undo.

define variable multiple    like mfc_logical label {&rcctwbc1_p_8} no-undo.

define variable nullstr as character no-undo.
define variable undotran as logical no-undo.
define variable i as integer no-undo.
define variable modulo_qty as integer no-undo.
define variable gwt_total like abs_gwt no-undo.
define variable vol_total like abs_vol no-undo.
define variable cont_qty_um like pt_um no-undo.
define variable error_flag as integer no-undo.
define variable l_nwt_total like abs_nwt no-undo.
define variable l_abs_tot_tare like abs_nwt    no-undo.

define variable l_pt_ship_wt   like pt_ship_wt label {&rcctwbc1_p_11} no-undo.
define variable l_cont_id      like abs_id     no-undo.

define variable lotnext like wo_lot_next.
define variable lotprcpt like wo_lot_rcpt no-undo.
define variable line_recno as recid.
define variable vShipToValue like so_ship no-undo.
define variable sales_order like abs_order no-undo.
define variable order_line like abs_line no-undo.
define variable return_container like mfc_logical no-undo.
define variable use-log-acctg as logical no-undo.
define variable l_FrTermsErr like mfc_logical no-undo.
define variable l_FrTermsOnFirstOrder like so_fr_terms no-undo.

/* TEMP TABLE */
define new shared temp-table work_sr_wkfl like sr_wkfl.

/* BUFFERS */
define buffer parent_container for abs_mstr.
define buffer abs_temp for abs_mstr.
define buffer abs_buff2 for abs_mstr.

/* VARIABLE DEFINITIONS FOR gpfile.i */
{gpfilev.i}

/* FORM FOR ADD CONTAINERS */

/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1.4 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1   COLUMN 3 NO-LABEL
 SKIP(.1)  /*GUI*/
beg_cont_num       colon 25
   skip(1)
   container_item     colon 25
   item_description   at 27    no-label
   item_description_2 at 27    no-label
   num_containers     colon 25
   cont_site          colon 58
   item_um            colon 25 label {&rcctwbc1_p_1}
   cont_loc           colon 58
   cont_lot_ser       colon 58
   l_pt_ship_wt       colon 25
   cont_wt_um                  no-label
   cont_ref           colon 58
   cont_vol           colon 25
   cont_vol_um                 no-label
   multiple           colon 58
   sales_order colon 25
   order_line colon 58
 SKIP(.4)  /*GUI*/
with frame a side-labels width 80
 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER.
 F-a-title = (getFrameTitle("CONTAINER_INFORMATION",30)).
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:WIDTH-PIXELS in frame a =
  FONT-TABLE:GET-TEXT-WIDTH-PIXELS(
  RECT-FRAME-LABEL:SCREEN-VALUE in frame a + " ", RECT-FRAME-LABEL:FONT).
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5. /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

/* CHECK IF LOGISTICS ACCOUNTING IS ENABLED */
{gprun.i ""lactrl.p"" "(output use-log-acctg)"}
/*GUI*/ if global-beam-me-up then undo, leave.


/*DETERMINE IF CONTAINER AND LINE CHARGES IS ENABLED */
{cclc.i}

assign
   issue_or_receipt = getTermLabel("ISSUE",8)
   /* INITIALIZE CENTRAL DATABASE ALIAS NAME */
   so_db = global_db
   abnormal_exit = true
   cline = global_db + "rcctwbc1.p".

/* ADD OR CREATE CONTAINERS */
main:
do:

   assign
      num_containers = 1
      beg_cont_num = "".

   display
      num_containers
   with frame a.

   get_data:
   do on endkey undo get_data, leave main
      on error undo get_data, retry get_data:
/*GUI*/ if global-beam-me-up then undo, leave.


      /* GET CONTAINER NUMBER */
      set
         beg_cont_num
      with frame a.

      /* GET NEXT AVAILABLE CONTAINER NUMBER FROM CONTROL FILE */
      if beg_cont_num = ""
      then do for mfc_ctrl transaction:
/*GUI*/ if global-beam-me-up then undo, leave.


         find mfc_ctrl where mfc_field = "rcc_next_ctnr"
         exclusive-lock no-wait no-error.

         /* HANDLE LOCKED CONTROL RECORD */
         if locked(mfc_ctrl) then do:
            {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=3}
            /* RECORD LOCKED - TRY AGAIN LATER */
            undo get_data, retry get_data.
         end.

         do while
         can-find(abs_mstr
            where abs_mstr.abs_shipfrom = ship_from
              and abs_mstr.abs_id = "C" + string(mfc_integer,"99999999")):
            mfc_integer = mfc_integer + 1.
            if mfc_integer >= 100000000 then
               mfc_integer = 1.
         end.

         assign
            beg_cont_num = string(mfc_integer,"99999999")
            mfc_integer = mfc_integer + 1.

         if mfc_integer >= 100000000 then
            mfc_integer = 1.

         display
            beg_cont_num
         with frame a.

      end.

      /* BEG_CONT_NUM <> "" SO FIND EXISTING CONTAINER */
      else do:

         find abs_mstr where
              abs_shipfrom = ship_from
          and abs_mstr.abs_id = "C" + beg_cont_num
         no-lock no-error.

         if available abs_mstr then do:
            /* CONTAINER NUMBER # HAS ALREADY BEEN CREATED */
            {pxmsg.i &MSGNUM=748 &ERRORLEVEL=3 &MSGARG1=abs_mstr.abs_id}
            undo get_data, retry get_data.
         end.

      end. /* FIND EXISTING CONTAINER */

      /* GET THE NUMBER OF CONTAINERS TO CREATE AND PART NUMBER */
      set_data:
      do on endkey undo get_data, retry get_data
         on error undo set_data, retry set_data:
/*GUI*/ if global-beam-me-up then undo, leave.


         set
            container_item
         with frame a editing:

            {mfnp.i pt_mstr
               container_item
               pt_part
               container_item
               pt_part
               pt_part}

            if recno <> ? then do:

               display
                  pt_part @ container_item
                  pt_desc1 @ item_description
                  pt_desc2 @ item_description_2
               with frame a.

            end.

         end. /* EDITING */
        /* IF CONTAINER CHARGES ARE IN USE GIVE AN ERROR IF THE CONTAINER */
        /* BEING SPECIFIED IS NOT DEFINED FOR CONTAINER CHARGES.          */

         if using_container_charges then do:
            for first ptc_det no-lock where
               ptc_part = container_item:
            end. /* FOR FIRST ptc_det */

            if not available ptc_det then do:
               /*ITEM HAS NOT BEEN DEFINED AS A CONTAINER*/
               {pxmsg.i &MSGNUM=4447 &ERRORLEVEL=3}
               undo, retry.
            end. /* IF NOT AVAILABLE ptc_det */
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* IF using_container_charges */

         assign
            new_site = ship_from
            global_part = container_item.

         /* VALIDATE THAT PART EXISTS AT THE SITE */
         {gprun.i ""gpptsi01.p""}
/*GUI*/ if global-beam-me-up then undo, leave.


         if err_stat = 2 then do:
            {pxmsg.i &MSGNUM=16 &ERRORLEVEL=3} /* PART NOT FOUND */
            next-prompt container_item with frame a.
            undo set_data, retry set_data.
         end.
      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* set_data */

      find pt_mstr where pt_part = container_item no-lock.

      display
         pt_desc1 @ item_description
         pt_desc2 @ item_description_2
         pt_um @ item_um
      with frame a.

      /* ASSIGN DEFAULTS */
      assign
         cont_loc     = pt_loc
         cont_qty_um  = pt_um
         cont_site    = ship_from
         l_pt_ship_wt = pt_ship_wt
         cont_wt_um   = pt_ship_wt_um
         cont_vol     = pt_size
         cont_vol_um  = pt_size_um
         multiple     = false
         sales_order = ""
         order_line = ""
         lotserial_control = pt_lot_ser.

      display
         cont_site
         cont_loc
         cont_lot_ser
         cont_ref
         l_pt_ship_wt
         cont_wt_um
         cont_vol
         cont_vol_um
         multiple
         sales_order
         order_line
      with frame a.

      /* GET INVENTORY DETAIL INFORMATION */
      inv_detail:
      do on endkey undo get_data, retry get_data
         on error undo inv_detail, retry inv_detail:
/*GUI*/ if global-beam-me-up then undo, leave.


         set
            num_containers
            l_pt_ship_wt
            cont_wt_um
            cont_vol
            cont_vol_um
            sales_order when(using_container_charges)
            cont_site
            cont_loc
            cont_lot_ser
            cont_ref
            multiple
            order_line when(using_container_charges)
         with frame a.

         /* IF USING CONTAINER CHARGES, VALIDATE SALES ORDER AND LINE */
         /* FIELDS   */

         if using_container_charges then do:
            /* VALIDATE SALES ORDER AND ORDER LINE  */
            if sales_order <> "" or order_line <> "" then do:
               for first so_mstr no-lock where
                  so_nbr = sales_order:
               end.
               if not available so_mstr then do:
                  {pxmsg.i &MSGNUM=609 &ERRORLEVEL=3}
                  /* SALES ORDER DOES NOT EXIST */
                  next-prompt sales_order with frame a.
                  undo inv_detail, retry inv_detail.
               end.

               for first sod_det no-lock where
                  sod_nbr = so_nbr  and
                  sod_line = integer(order_line):
               end.
               if not available sod_det then do:
                  {pxmsg.i &MSGNUM=764 &ERRORLEVEL=3}
                  /* SALES ORDER LINE DOES NOT EXIST */
                  next-prompt order_line with frame a.
                  undo inv_detail, retry inv_detail.
               end.

               if sod_site <> ship_from then do:
                  {pxmsg.i &MSGNUM=8228
                           &ERRORLEVEL=3
                           &MSGARG1=sod_site}
                  /* INVALID ORDER SHIP-FROM SITE */
                  next-prompt sales_order with frame a.
                  undo inv_detail, retry inv_detail.
               end.
               vShipToValue = (if sod_dock <> ""
                               then sod_dock else
                               if sod_ship <> "" then sod_ship
                               else so_ship).
               if vShipToCode <> "" and vShipToCode <> vShipToValue
               then do:
                  {pxmsg.i &MSGNUM=8229
                           &ERRORLEVEL=3
                           &MSGARG1=sod_ship}
                   /* INVALID ORDER SHIP-TO */
                  next-prompt sales_order with frame a.
                  undo inv_detail, retry inv_detail.
               end.
               if vShipToCode = "" then vShipToCode = vShipToValue.

             /* SALES ORDER AND LINE CAN ONLY BE ENTERED WHEN USING CONTAINER */
             /* CHARGES MODULE. VALIDATE FR TERMS IF LOGISTICS ACCOUNTING IS  */
             /* ENABLED.                                                      */
              if use-log-acctg then do:
                 /* VALIDATE SALES ORDER FREIGHT TERMS FOR THIS CONTAINER */
                 run validateSOFrTerms (input ship_from,
                                        input temp_parent,
                                        input so_fr_terms,
                                        output l_FrTermsOnFirstOrder,
                                        output l_FrTermsErr).
                 if l_FrTermsErr then do:
                    /* ALL ATTACHED ORDERS MUST HAVE FREIGHT TERMS # */
                    {pxmsg.i &MSGNUM=5056 &ERRORLEVEL=3
                             &MSGARG1= l_FrTermsOnFirstOrder}
                    next-prompt sales_order with frame a.
                    undo inv_detail, retry inv_detail.
                 end. /* IF l_FrTermsErr */
              end.   /* if use-log-acctg */

            end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* IF sales_order <> "" or order_line ... */
         end. /* IF using_container_charges */

         if num_containers < 0 then do:
            if not using_container_charges then do:
               {pxmsg.i &MSGNUM=3195 &ERRORLEVEL=3}
               /* Quantity must be greater than 0 */
               undo inv_detail, retry inv_detail.
            end.
            assign
               num_containers = num_containers * -1
               return_container = true.
         end.
         else return_container = false.

         if num_containers = 0 then do:
            {pxmsg.i &MSGNUM=317 &ERRORLEVEL=3} /* CANNOT BE ZERO */
            undo inv_detail, retry inv_detail.
         end.

         /* DELETE OLD SR_WKFL RECORDS */
         for each sr_wkfl where sr_userid = mfguser
         exclusive-lock:
            delete sr_wkfl.
         end.

         for each lotw_wkfl
            where lotw_mfguser = mfguser
            exclusive-lock:
            delete lotw_wkfl.
         end. /* FOR EACH lotw_wkfl */


         /* SWITCH TO THE REMOTE SITE IF NECESSARY */
         {gprun.i ""gpalias2.p""
            "(input cont_site,
              output error_flag)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if error_flag <> 0 and error_flag <> 9 then do:
            /* UNABLE TO CONNECT TO REMOTE DATABASE */
            {pxmsg.i &MSGNUM = 2510 &ERRORLEVEL = 4
                     &MSGARG1 = getTermLabel(""FOR_REMOTE_INVENTORY"",30)}
            undo inv_detail, retry inv_detail.
         end.

         /* DELETE OLD SR_WKFL RECORDS */
         for each sr_wkfl where sr_userid = mfguser
         exclusive-lock:
            delete sr_wkfl.
         end.

         for each lotw_wkfl
            where lotw_mfguser = mfguser
            exclusive-lock:
            delete lotw_wkfl.
         end. /* FOR EACH lotw_wkfl */

         if not multiple then do:

            /* VALIDATE THE INVENTORY DETAIL INFORMATION */
            {gprun.i ""rcctmtb.p""
               "(input transtype,
                 input ship_from,
                 input cont_site,
                 input cont_loc,
                 input container_item,
                 input cont_lot_ser,
                 input cont_ref,
                 input num_containers,
                 input cont_qty_um,
                 input 0,
                 output undotran)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            if undotran then do:
               /* RESET TO CENTRAL DATABASE IF NECESSARY */
               {gprun.i ""gpalias3.p""
                  "(input so_db, output error_flag)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               undo inv_detail, retry inv_detail.
            end.

         end. /* IF NOT MULTIPLE */

         /* SET THE MULTIPLE INFO IF NECESSARY */
         if multiple then do:

            assign
               cline = ""
               site = cont_site
               location = cont_loc
               lotserial = cont_lot_ser
               lotserial_qty = num_containers
               trans_um = cont_qty_um
               trans_conv = 1
               lotnext = ""
               lotprcpt = no
               lotref = cont_ref.

            /* MULTIPLE ENTRY ROUTINE */
            /* ADDED SIXTH INPUT PARAMETER AS NO */
            {gprun.i ""icsrup.p""
               "(input ship_from,
                 input """",
                 input """",
                 input-output lotnext,
                 input lotprcpt,
                 input no)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            /* HANDLE NO MULTIPLE ENTRY RECORDS CREATED CONDITION */
            find first sr_wkfl where sr_userid = mfguser
            no-lock no-error.

            if not available sr_wkfl then do:
               /* RESET TO CENTRAL DATABASE IF NECESSARY */
               {gprun.i ""gpalias3.p""
                  "(input so_db, output error_flag)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               undo inv_detail, retry inv_detail.
            end.

         end. /* IF MULTIPLE */

         /* SET WORK_SR_WKFL WORKFILE RECORDS */
         {gprun.i ""rcshmtb2.p""
            "(input mfguser, input cline)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         /* RESET TO CENTRAL DATABASE IF NECESSARY */
         {gprun.i ""gpalias3.p""
            "(input so_db, output error_flag)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      end. /* inv_detail */

   end. /* get_data */

   do transaction:

      /* CREATE WORK_SR_WKFL FOR NOT MULTI-ENTRY */
      find first work_sr_wkfl where work_sr_wkfl.sr_userid = mfguser
      no-lock no-error.

      if not available work_sr_wkfl then do:
         create work_sr_wkfl.
         assign
            work_sr_wkfl.sr_userid = mfguser
            work_sr_wkfl.sr_site = cont_site
            work_sr_wkfl.sr_loc = cont_loc
            work_sr_wkfl.sr_lotser = cont_lot_ser
            work_sr_wkfl.sr_ref = cont_ref
            work_sr_wkfl.sr_qty = num_containers.
      end.

   end. /* DO TRANSACTION */

   num_containers = 0.

   create_containers:
   do for mfc_ctrl transaction:
/*GUI*/ if global-beam-me-up then undo, leave.


      /* CREATE THE ALLOTED NUMBER OF CONTAINER RECORDS */
      /* FROM THE SR_WKFL RECORDS */
      for each work_sr_wkfl where work_sr_wkfl.sr_userid = mfguser
      exclusive-lock:
/*GUI*/ if global-beam-me-up then undo, leave.


         do i = 1 to work_sr_wkfl.sr_qty:

            if beg_cont_num = "" then do:

               /* GET NEXT CONTAINER NUMBER */
               find mfc_ctrl where mfc_field = "rcc_next_ctnr"
               exclusive-lock no-wait no-error.

               if locked(mfc_ctrl) then do:
                  {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=3}
                  /* RECORD LOCKED -TRY AGAIN LATER*/
                  undo create_containers, leave main.
               end.

               do while can-find(abs_mstr
                           where abs_mstr.abs_shipfrom = ship_from
                             and abs_mstr.abs_id = "C" +
                                 string(mfc_integer,"99999999")):
                  assign
                     mfc_integer = mfc_integer + 1.
                  if mfc_integer >= 100000000 then
                     mfc_integer = 1.
               end.

               assign
                  beg_cont_num = string(mfc_integer,"99999999")
                  mfc_integer = mfc_integer + 1.

               if mfc_integer >= 100000000 then
                  mfc_integer = 1.

            end.

            /* CREATE THE CONTAINER RECORD */
            l_cont_id = "C" + beg_cont_num.

            run p_create_cont
               (input ship_from,
                input temp_parent,
                input l_cont_id,
                input parent_number,
                input container_item,
                input work_sr_wkfl.sr_loc,
                input work_sr_wkfl.sr_lotser,
                input cont_level,
                input work_sr_wkfl.sr_ref,
                input cont_wght,
                input cont_wt_um,
                input cont_vol,
                input cont_vol_um,
                input work_sr_wkfl.sr_site,
                input cont_qty_um,
                input l_pt_ship_wt,
                input sales_order,
                input order_line,
                input return_container,
                output line_recno).

            assign
               num_containers = num_containers + 1
               beg_cont_num = "".
            if (using_container_charges or using_line_charges) then do:
               run CreateUserFields (input ship_from,
                                     input vShipToCode,
                                     input l_cont_id).

               run getUserFieldData (input l_cont_id, input ship_from).
            end. /*IF (USING_CONTAINER_CHARGES*/

         end. /* DO I = 1 TO SR_QTY */

         delete work_sr_wkfl.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* FOR EACH SR_WKFL */

      /* ROLLING UP TARE WEIGHTS OF CONTAINERS CREATED TO ITS PARENT */
      for first abs_buff2
      fields (abs_cmtindx abs_fa_lot abs_gwt abs_id abs_item
              abs_line abs_loc abs_lotser abs_nwt abs_order
              abs_par_id abs_qty abs_ref abs_shipfrom abs_shipto
              abs_shp_date abs_site abs_type abs_vol abs_vol_um
              abs__qad02 abs__qad03 abs__qad06 abs__qad10)
      where abs_buff2.abs_shipfrom = ship_from and
            abs_buff2.abs_id       = parent_number
      no-lock: end.
/*GUI*/ if global-beam-me-up then undo, leave.


      if available abs_buff2 then do:
         /* ROLLING UP TARE WEIGHT TO PARENT */
         {gprun.i ""icshnwt.p""
            "(input recid(abs_buff2),
              input l_abs_tot_tare,
              input no,
              input cont_wt_um)"}
/*GUI*/ if global-beam-me-up then undo, leave.

      end. /* IF AVAILABLE ABS_BUFF2 */

      i = 0.

      /* COUNT THE NUMBER OF CHILD CONTAINERS THAT NEED TO BE       */
      /* ASSIGNED TO THIS PARENT LEVEL AND SAVE IN i                */
      for each abs_mstr where
               abs_mstr.abs_shipfrom = ship_from
           and abs_mstr.abs_shipto = temp_parent
           and integer(abs_mstr.abs__qad06) = cont_level - 1
      no-lock:
         i = i + 1.
      end.

      /* AT THIS POINT i CONTAINS THE NUMBER OF CHILD LEVEL RECORDS */
      /* AND NUM_CONTAINERS CONTAINS THE NUMBER OF NEWLY CREATED    */
      /* PARENT RECORDS                                             */

      /* QTY TO ASSIGN TO EACH PARENT */
      assign
         cont_qty = (i - (i modulo num_containers)) / num_containers
         modulo_qty = i modulo num_containers.

      /* AT THIS POINT CHILD CONTAINERS ARE IDENTIFIED BY THE        */
      /*   RELATIONSHIP OF ABS_SHIPTO = TEMP_PARENT WITH CONT LEVEL  */
      /*   OF 1 LESS THAN THE PARENT CONTAINERS AND PARENT           */
      /*   CONTAINERS THAT THEY ARE TO BE ASSIGNED TO ARE IDENTIFIED */
      /*   BY THE RELATIONSHIP OF ABS_SHIPTO = TEMP_PARENT WITH THIS */
      /*   CONT_LEVEL.                                               */

      find first parent_container
           where parent_container.abs_shipfrom = ship_from
             and parent_container.abs_shipto = temp_parent
             and integer(parent_container.abs__qad06) = cont_level
      exclusive-lock no-error.

      /* CHILD CONTAINER */
      find first abs_mstr where
                 abs_mstr.abs_shipfrom = ship_from
             and abs_mstr.abs_shipto = temp_parent
             and integer(abs_mstr.abs__qad06) = (cont_level - 1)
      exclusive-lock no-error.

      /* ASSIGN CHILD CONTAINERS TO PARENT CONTAINERS */
      assign_child:
      repeat while available parent_container and available abs_mstr:

         /* ASSIGN i THE NUMBER OF CHILD RECORDS TO ASSIGN */
         i = cont_qty.

         /* ASSIGN i NUMBER OF CHILD CONTAINERS TO THIS PARENT */
         do while i > 0 or modulo_qty > 0:

            if available abs_mstr then do:

               /* PLACE MODULO ON FIRST RECORD ONLY */
               if modulo_qty > 0 then
                  modulo_qty = modulo_qty - 1.
               else
                  i = i - 1.

               /* ASSIGN THE PARENT ID TO THE RECORD */
               abs_mstr.abs_par_id = parent_container.abs_id.

            end. /* IF AVAILABLE ABS_MSTR */

            else
               leave assign_child.

            find next abs_mstr where
                      abs_mstr.abs_shipfrom = ship_from
                  and abs_mstr.abs_shipto = temp_parent
                  and integer(abs_mstr.abs__qad06) = (cont_level - 1)
            exclusive-lock no-error.

         end.

         /* ADD WEIGHTS OF CHILD RECORDS */
         assign
            l_nwt_total = 0
            gwt_total = 0.

         for each abs_temp
            where abs_temp.abs_shipfrom = parent_container.abs_shipfrom
              and abs_temp.abs_par_id = parent_container.abs_id
         no-lock:
            assign
               l_nwt_total = l_nwt_total + abs_nwt
               gwt_total = gwt_total + abs_gwt.
         end.

         /* CONTAINER NET WT IS THE SUM OF NET WTS OF THE CHILD RECORDS */
         assign
            parent_container.abs_nwt = l_nwt_total
            parent_container.abs_gwt =
            parent_container.abs_gwt + gwt_total.

         /* FIND THE NEXT PARENT TO ASSIGN CHILD RECORDS TO AND LOOP */
         /* BACK UP UNTIL EITHER THE TOTAL NUMBER OF CHILD RECORDS   */
         /* PER PARENT RECORD IS ASSIGNED OR NO MORE CHILD RECORDS   */
         /* ARE AVAILABLE TO ASSIGN                                  */
         find next parent_container
             where parent_container.abs_shipfrom = ship_from
               and parent_container.abs_shipto = temp_parent
               and integer(parent_container.abs__qad06) = cont_level
         exclusive-lock no-error.

      end. /* assign_child */

   end. /* create_containers */

   abnormal_exit = false.

end. /* main */

hide frame a no-pause.

PROCEDURE p_create_cont:
/*--------------------------------------------------------------------
  Purpose:     Created Container Records
  Parameters:
  Notes:       Rollup of container weights is not done here
---------------------------------------------------------------------*/
   define input parameter cont_shipfrom    like abs_shipfrom.
   define input parameter cont_shipto      like abs_shipto.
   define input parameter cont_id          like abs_id.
   define input parameter cont_parid       like abs_par_id.
   define input parameter cont_item        like abs_item.
   define input parameter cont_loc         like abs_loc.
   define input parameter cont_lot         like abs_lot.
   define input parameter cont_level       as integer.
   define input parameter cont_ref         like abs_ref.
   define input parameter cont_wt          like abs_nwt.
   define input parameter cont_wt_um       like abs_wt_um.
   define input parameter cont_vol         like abs_vol.
   define input parameter cont_vol_um      like abs_vol_um.
   define input parameter cont_site        like abs_site.
   define input parameter cont_qty_um      like abs__chr02.
   define input parameter cont_gwt         like abs_gwt.
   define input parameter sales_order      like abs_order no-undo.
   define input parameter order_line       like abs_line no-undo.
   define input parameter ip_return        like mfc_logical no-undo.

   /* OUTPUT PARAMETERS */
   define output parameter line_recno as recid.

   /* LOCAL VARIABLES */
   define variable l_abs_tare_wt    like abs_nwt     no-undo.
   define variable cont_qty         like abs_qty     no-undo.
   define variable cont_child       like abs_id      no-undo.
   define variable cmtindx          like abs_cmtindx no-undo.
   define variable cont_qty_conv    as decimal       no-undo.
   define variable cont_order       like abs_order   no-undo.
   define variable cont_line        like abs_line    no-undo.
   define variable part_fa_lot      like wo_lot      no-undo.

   /* BUFFERS */
   define buffer child_container for abs_mstr.

   assign
      cont_qty      = if ip_return then -1 else 1
      cont_child    = "NONE"
      cmtindx       = 0
      cont_qty_conv = 1
      cont_order    = (if using_container_charges then sales_order else '')
      cont_line     = (if using_container_charges then order_line  else '')
      part_fa_lot   = "".

   create abs_mstr.
   assign
      abs_mstr.abs_shipfrom = cont_shipfrom
      abs_mstr.abs_id       = cont_id
      abs_mstr.abs_shipto   = cont_shipto
      abs_mstr.abs_par_id   = cont_parid
      abs_mstr.abs_qty      = cont_qty
      abs_mstr.abs_shp_date = today
      abs_mstr.abs_type     = "s"
      abs_mstr.abs_item     = cont_item
      abs_mstr.abs_loc      = cont_loc
      abs_mstr.abs_lot      = cont_lot
      abs_mstr.abs__qad06   = string(cont_level,"99")
      abs_mstr.abs_ref      = cont_ref
      abs_mstr.abs_gwt      = maximum(cont_gwt * cont_qty,0)
      abs_mstr.abs_vol      = maximum(cont_vol * cont_qty * cont_qty_conv, 0)
      abs_mstr.abs_vol_um   = cont_vol_um
      abs_mstr.abs_site     = cont_site
      abs_mstr.abs__qad02   = cont_qty_um
      abs_mstr.abs__qad03   = string(cont_qty_conv)
      abs_cmtindx           = cmtindx
      abs_mstr.abs_order    = cont_order
      abs_mstr.abs_line     = cont_line
      abs_mstr.abs_wt_um    = cont_wt_um
      abs_mstr.abs_fa_lot   = part_fa_lot.

   assign
      l_abs_tare_wt       = maximum (cont_gwt * cont_qty,0).

   {abspack.i "abs_mstr" 26 22 "l_abs_tare_wt" }

   line_recno = recid(abs_mstr).

   /* ACCUMULATING TARE WEIGHTS OF CHILD CONTAINERS BEING CREATED   */
   /* TO BE ROLLED UP TO ITS PARENT                                 */
   assign
      l_abs_tot_tare = l_abs_tot_tare + l_abs_tare_wt.

   if cont_child <> "NONE"
   then do:

      find child_container
         where child_container.abs_shipfrom = cont_shipfrom
           and child_container.abs_id = cont_child
      exclusive-lock no-error.

      if available child_container then
         child_container.abs_par_id = abs_mstr.abs_id.

   end. /* IF CONT_CHILD <> "NONE" */

   if recid(abs_mstr) = -1 then.

END PROCEDURE. /* PROCEDURE P_CREATE_CONT */

PROCEDURE CreateUserFields:
   define input parameter ipShipFrom like abs_shipfrom no-undo.
   define input parameter ipShipTo like abs_shipto no-undo.
   define input parameter ip_abs_id like abs_id no-undo.
   /*CREATE LINE LEVEL USER FIELDS*/
   {gprunmo.i
          &program = ""sosob1b.p""
          &module  = "ACL"
          &param   = """(input ip_abs_id,
                         input ipShipFrom,
                         input ipShipTo,
                         input 2)"""}

END PROCEDURE.


PROCEDURE getUserFieldData:
   define input parameter ipAbsID like abs_id.
   define input parameter ipShipFrom like abs_shipfrom.

   define variable vFieldCounter as integer no-undo.
   define variable vFieldList as character no-undo.
   define variable vFieldName as character no-undo.


   vFieldList = "container_item,num_containers," +
                "l_pt_ship_wt,cont_wt_um,cont_vol," +
                "cont_vol_um,sales_order,cont_site," +
                "cont_loc,cont_lot_ser,cont_ref," +
                "multiple,order_line".

   do vFieldCounter = 1 to num-entries(vFieldList,","):
      vFieldName = entry(vFieldCounter,vFieldList,",").
      if can-find(first absd_det where
             absd_abs_id = ipAbsID
         and absd_shipfrom = ipShipFrom
         and absd_abs_fld_name = vFieldName
         and absd_fld_prompt = yes)
      then do:
         {gprunmo.i
            &module = "ACL"
            &program = ""rcswbuf.p""
            &param   = """(input yes,
                           input vFieldName,
                           input ipAbsID,
                           input ipShipFrom)"""}
      end. /*IF CAN-FIND*/
   end. /* DO vFieldCounter = 1 TO */

   if can-find(first absd_det where
         absd_abs_id = ipAbsID
     and absd_shipfrom = ipShipFrom
     and absd_abs_fld_name = ""
     and absd_fld_prompt = yes)
   then do:

     {gprunmo.i
        &module  = "ACL"
        &program = ""rcswbuf.p""
        &param   = """(input no,
                       input "''",
                       input ipAbsID,
                       input ipShipFrom)"""}
   end. /*IF CAN_FIND */
END PROCEDURE. /*getUserFieldData*/

{rcinvcon.i} /* PROCEDURE get_abs_parent IS DEFINED IN THIS INCLUDE */

PROCEDURE validateSOFrTerms:
/*--------------------------------------------------------------------
   Purpose:     Get the highest level parent abs_mstr record
                Get the first order attached to this parent along with
                   the freight terms on this order.
                Compare the terms on this order to that of the input parameter
                  ip_OrderFrTerms and return op_FrTermsErr if they are
                  different.
   Parameters:
   Notes:
----------------------------------------------------------------------*/
   define input parameter ip_ShipFrom   as   character    no-undo.
   define input parameter ip_TempParent   as   character    no-undo.
   define input parameter ip_OrderFrTerms like so_fr_terms  no-undo.

   define output parameter op_FrTermsOnFirstOrder as character  no-undo.
   define output parameter op_FrTermsErr like mfc_logical  no-undo.

   define buffer b1_abs_mstr for abs_mstr.
   define buffer b2_abs_mstr for abs_mstr.

   define variable par_absid like abs_id no-undo.
   define variable par_shipfrom like abs_shipfrom no-undo.
   define variable l_FirstOrder like so_nbr no-undo.

   l_FirstOrder = "".

   for first b1_abs_mstr
   where recid(b1_abs_mstr) =
   integer(substring(ip_TempParent,(index(ip_TempParent,"::") + 2)))
   no-lock:
   end.

   if not available b1_abs_mstr then
      for first b1_abs_mstr
      where b1_abs_mstr.abs_shipfrom = ip_ShipFrom
        and b1_abs_mstr.abs_shipto = ip_TempParent
      no-lock:
      end.

   if available b1_abs_mstr then do:
      run get_abs_parent (input b1_abs_mstr.abs_id,
                          input b1_abs_mstr.abs_shipfrom,
                          output par_absid,
                          output par_shipfrom).
   end. /* IF AVAILABLE B1_ABS_MSTR */

   if par_absid <> ? then
   do for b2_abs_mstr:
      for first b2_abs_mstr where b2_abs_mstr.abs_id = par_absid and
         b2_abs_mstr.abs_shipfrom = par_shipfrom
         no-lock:
      end.
      if available b2_abs_mstr then do:
         {gprunmo.i  &module = "LA" &program = "lashex01.p"
                     &param  = """(buffer b2_abs_mstr,
                                   output l_FirstOrder,
                                   output op_FrTermsOnFirstOrder)"""}

         /* IF THERE IS AN ORDER ATTACHED TO THE PARENT ABS_MSTR,   */
         /* COMPARE THE FREIGHT TERMS ON THAT ORDER TO THOSE ON THE */
         /* ORDER BEING ATTACHED.                                   */

         if l_FirstOrder <> "" and
            op_FrTermsOnFirstOrder <> ip_OrderFrTerms then
         do:
            op_FrTermsErr = true.
            return.
         end. /* IF l_FirstOrder <> "" AND... */

      end. /* IF AVAILABLE b2_abs_mstr */
   end. /* DO FOR B2_ABS_MSTR */

END PROCEDURE. /* validateSOFrTerms */
